package hist.mobilprogramming.tictactoeapplication;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RoundContainerTests
{
	RoundContainer _gameRounds;
	int _roundsToPlay = 2;
	int _players = 2;
	
	@Before 
    public void setUp() 
    { 
    	_gameRounds = new RoundContainer(_players, _roundsToPlay);
    } 

    /* Tears down the test fixture. 
     * (Called after every test case method.) 
     */ 
    @After 
    public void tearDown() 
    { 
         _gameRounds = null;
    } 
    
    @Test
	public void Terminated_WhenGameWithSingleRoundIsCompleted_ReturnTrue() throws ApplicationException
	{    	
    	RoundContainer gameRounds = new RoundContainer(_players);
    	gameRounds.increaseCompleted();
    	
    	boolean result = gameRounds.terminated();
    	
		assertEquals(true, result);
	}

    @Test
	public void Terminated_WhenFirstRoundIsDrawOfGameWithTwoRounds_ReturnFalse() throws ApplicationException
	{   
    	_gameRounds.increaseCompleted();
    	
    	boolean result = _gameRounds.terminated();
    	
		assertEquals(false, result);
	}

    @Test
	public void Terminated_WhenTwoDrawRoundsOfGameWithTwoRounds_ReturnFinished() throws ApplicationException
	{   
    	_gameRounds.increaseCompleted();
    	_gameRounds.increaseCompleted();
    	
    	boolean result = _gameRounds.terminated();
    	
		assertEquals(true, result);
		assertEquals(2, _gameRounds.getCompleted());
	}

    @Test
	public void GetResult_WhenNoneRoundsCompleted_ReturnZeroResultForPlayer() throws ApplicationException
	{   
    	Integer[] expected = new Integer[] {1, 0, 0};
    	
    	Integer[] result = _gameRounds.getResult(1);
    	
    	assertArrayEquals(expected, result);
	}
    
    @Test  (expected=ApplicationException.class)
	public void Completed_WhenReachNumberOfRoundsLimit_ThrowsException() throws ApplicationException
	{	    	
    	_gameRounds.increaseCompleted();
    	_gameRounds.increaseCompleted();
    	_gameRounds.increaseCompleted();
	}
    
    @Test
	public void GetResult_WhenGameWithThreeRoundsWhereTwoWinsAndOneLoss_ReturnResultForPlayerX() throws ApplicationException
	{   
    	Integer[] expected = new Integer[] {1, 2, 1};
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted(Player.X.ordinal());
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	Integer[] result = rounds.getResult(Player.X.ordinal());
    	
    	assertArrayEquals(expected, result);
	}
    
    @Test
	public void GetResult_WhenGameWithThreeRoundsWhereOneWinAndTwoLoss_ReturnResultForPlayerO() throws ApplicationException
	{   
    	Integer[] expected = new Integer[] {2, 1, 2};
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted(Player.X.ordinal());
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	Integer[] result = rounds.getResult(Player.O.ordinal());
    	
    	assertArrayEquals(expected, result);
	}
   
    @Test
	public void GetResultText_WhenGameWithTwoRoundsWhereOneRoundFinished_ReturnResultForPlayerX() throws ApplicationException
	{   
    	RoundContainer rounds = new RoundContainer(_players, 2);
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	String result = rounds.getResultText(Player.X.ordinal());
    	
    	assertEquals("1 - 0", result);
	}
    
    @Test
	public void Terminated_WhenGameWithThreeRounds_ReturnTrue() throws ApplicationException
	{
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted(Player.X.ordinal());
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	boolean result = rounds.terminated();
    	
    	assertEquals(true, result);
		assertEquals(3, rounds.getCompleted());
	}
    
    @Test
	public void Terminated_WhenGameWithThreeRoundsWhereTwoOfThemIsCompleted_ReturnFalse() throws ApplicationException
	{
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted();
    	rounds.increaseCompleted(Player.O.ordinal());
    	
    	boolean result = rounds.terminated();
    	
    	assertEquals(false, result);
		assertEquals(2, rounds.getCompleted());
	}
    
    @Test
	public void GetWinnerId_WhenGameWithThreeRoundsWhereTwoOfThemIsCompleted_ReturnZero() throws ApplicationException
	{
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted();
    	rounds.increaseCompleted(Player.O.ordinal());
    	
    	int result = rounds.getWinnerId();
    	
    	assertEquals(0, result);
	}
    
    @Test
	public void GetWinnerId_WhenGameWithThreeRoundsWhereAllCompletedWithFinalWinner_ReturnWinnerId() throws ApplicationException
	{
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	int result = rounds.getWinnerId();
    	
    	assertEquals(2, result);
	}
    
    @Test
	public void GetWinnerId_WhenGameWithThreeRoundsWhereCompletedAsOneDrawAndOneWinToEachPlayer_ReturnZero() throws ApplicationException
	{
    	RoundContainer rounds = new RoundContainer(_players, 3);
    	rounds.increaseCompleted();
    	rounds.increaseCompleted(Player.O.ordinal());
    	rounds.increaseCompleted(Player.X.ordinal());
    	
    	int result = rounds.getWinnerId();
    	
    	assertEquals(0, result);
	}
    
    /*@Test
	public void GetResults_WhenGameWithThreeRoundsFinished_ReturnResultsForPlayersO() throws ApplicationException
	{   
    	ArrayList<Integer[]> expected = new ArrayList<Integer[]>();
    	expected.add(new Integer[] {1, 0, 3});
    	expected.add(new Integer[] {2, 3, 0});
    	GameRoundsManager rounds = new GameRoundsManager(_players, 3);
    	rounds.completed(Player.X.ordinal());
    	rounds.completed(Player.X.ordinal());
    	rounds.completed(Player.X.ordinal());
    	
    	ArrayList<Integer[]> result = rounds.getResults();
    	
    	assertEquals(expected.size(), result.size());
    	assertEquals(expected.get(0), result.get(0));
    	//assertTrue(expected.equals(result));
	}*/
}
